﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Security.Cryptography;

namespace MvcEngine.Mvc
{
    public class AntiForgeryToken
    {
        public static readonly string TicksName = "__AntiForgeryTicks";
        public static readonly string TokenName = "__AntiForgeryToken";

        private readonly string salt;

        public AntiForgeryToken(string salt)
        {
            this.salt = salt;
            Ticks = getTicks();
        }

        public long Ticks { get; set; }

        public string Value
        {
            get
            {
                return GetHash(salt, Ticks.ToString());
            }
        }

        private static long getTicks()
        {
            return DateTime.UtcNow.Ticks;
        }

        public static string GetHash(string salt, string ticks)
        {
            byte[] bytesToHash = Encoding.UTF8.GetBytes(string.Concat(salt, ticks));

            SHA512 hasher = new SHA512Managed();
            byte[] hashed = hasher.ComputeHash(bytesToHash);

            return Convert.ToBase64String(hashed);
        }
    }
}
